package math.mathExtended;

/**
 *
 * Fully Tested: No
 * Fully Documented: No
 * Created: 7-May-2012
 * Last Updated: 7-May-2012
 *
 * @author Shimu Wu
 */
public class DoubleComparator {
    
    private double EPSILEON;
    
    public DoubleComparator(){
        this(0.0000001);
    }
    
    public DoubleComparator(double epsileon) {
        this.EPSILEON = epsileon;
    }
    
    /**
     * @return the EPSILEON
     */
    public double getEpsileon() {
        return EPSILEON;
    }

    /**
     * @param epsileon the EPSILEON to set
     */
    public void setEpsileon(double epsileon) {
        this.EPSILEON = epsileon;
    }
    
    /**
     * Returns true if |arg1 - arg2| &lt; EPSILEON, in other words, returns
     * if arg1 is equal to arg2 within the allowed EPSILEON margin of error.
     * Should be used instead of the regular == comparison, due to possibility
     * of floating point round offs. The EPSILEON used by this method can
     * be retrieved using getEpsileon().
     *
     * @param arg1 the first value
     * @param arg2 the second value
     * @return true if arg1 is equal to arg2 within the given error allowance
     */
    public boolean equal(double arg1, double arg2) {
        return (Math.abs(arg1 - arg2) < EPSILEON);
    }

    public boolean equal(double[] array1, double[] array2) {
        if (array1.length != array2.length) {
            return false;
        }
        
        for (int i = 0; i < array1.length; i++) {
            if (!equal(array1[i], array2[i])) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * Returns true if num1 is less than num2, false otherwise
     *
     * @param num1
     * @param num2
     * @return
     */
    public boolean lessThanOrEqual(double num1, double num2) {
        if ((num1 < num2) || (equal(num1, num2))) {
            return true;
        }
        return false;
    }

    /**
     * Returns true if num1 is greater than num2, false otherwise
     *
     * @param num1
     * @param num2
     * @return
     */
    public boolean greaterThanOrEqual(double num1, double num2) {
        if ((num1 > num2) || (equal(num1, num2))) {
            return true;
        }
        return false;
    }
    
}
